package demo;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.IOException;

/**
 * swing托盘图标、frame居中显示
 */
public class AstCenterTrayIcon extends JFrame implements ActionListener {
    private static final long serialVersionUID = -5102211705496305088L;

    private PopupMenu pop;
    private MenuItem open, close;
    private TrayIcon trayicon;
    private JFrame jframe;

    public AstCenterTrayIcon() throws HeadlessException {
        super();
        initComponents();
    }

    public AstCenterTrayIcon(GraphicsConfiguration gc) {
        super(gc);
        initComponents();
    }

    public AstCenterTrayIcon(String title, GraphicsConfiguration gc) {
        super(title, gc);
        initComponents();
    }

    public AstCenterTrayIcon(String title) throws HeadlessException {
        super(title);
        initComponents();
    }

    public void initComponents() {
        jframe = this;

        setBackground(Color.lightGray);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        getContentPane().setLayout(new BorderLayout());

        setResizable(false);
        Image image = Toolkit.getDefaultToolkit().getImage(
                this.getClass().getResource("logo.png"));

        this.setIconImage(image);

        //创建弹出菜单
        pop = new PopupMenu();
        open = new MenuItem("Open");
        open.addActionListener(this);
        close = new MenuItem("Close");
        close.addActionListener(this);
        pop.add(open);
        pop.add(close);

        if (SystemTray.isSupported()) {// 判断系统是否托盘
            SystemTray tray = SystemTray.getSystemTray();//获取系统托盘

            //创建托盘图标，并赋予事件
            trayicon = new TrayIcon(image, "AstCenter系统", pop);
            trayicon.addMouseListener(new MouseAdapter() {
                public void mouseClicked(MouseEvent e) {
                    if (e.getClickCount() == 2) {
                        showFrame();
                    }
                }
            });

            try {
                tray.add(trayicon);//将托盘图标添加到系统托盘
            } catch (AWTException e) {
                e.printStackTrace();
            }

        }


    }

    //状态切换效果
    public void showFrame() {
        jframe.setState(Frame.ICONIFIED);
        jframe.setVisible(true);
        jframe.setState(Frame.NORMAL);
    }


    @Override
    public void actionPerformed(ActionEvent e) {
        if (e.getSource() == open) {
            showFrame();
        }
        if (e.getSource() == close) {
            jframe.dispatchEvent(new WindowEvent(jframe,
                    WindowEvent.WINDOW_CLOSED));
        }
    }

    @Override
    protected void processWindowEvent(WindowEvent e) {
        super.processWindowEvent(e);
        if (e.getID() == WindowEvent.WINDOW_CLOSED) {
            int option = JOptionPane.showConfirmDialog(null, "您确认要关闭系统吗？",
                    "确认提示", JOptionPane.OK_OPTION | JOptionPane.CANCEL_OPTION);
            if (option == JOptionPane.OK_OPTION) {
                System.exit(0);
            }
        } else if (e.getID() == WindowEvent.WINDOW_ICONIFIED) {//最小化时隐藏iframe，模拟隐藏任务栏图标
            setVisible(false);
        }
    }

    public static void main(String[] args) throws IOException {
        AstCenterTrayIcon astCenter = new AstCenterTrayIcon("AstCenter系统");
        astCenter.pack();
        Dimension local_size = new Dimension(300, 200);
        astCenter.setSize(local_size);
        astCenter.setLocationRelativeTo(null);
        astCenter.setVisible(true);
    }


}